<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { getAllPage,getDetail } from '../api/product.js';
import router from '@/router';

const productList = ref([]) //查询的商品列表
const currentPage = ref(1)  //当前页数
const pages = ref(1) //总共的页数

const searchName = ref("") //搜索框的商品名称
const searchMinPrice = ref(null)//搜索框的最低价格
const searchMaxPrice = ref(null)//搜索框的最高价格

onMounted(() => {
  getAll(1)
})
//分页查询商品
async function getAll(pageNumber: number) {
  const requestParams = {
    pageNumber,
    proName: searchName.value,
    MinPrice: searchMinPrice.value,
    MaxPrice: searchMaxPrice.value,
  }
  const response = await getAllPage(requestParams)
  productList.value = response.data.data.records 
  currentPage.value = response.data.data.current
  pages.value = response.data.data.pages
}
//单击查询商品
async function getProductDetail(id) {
    console.log("-----------单击商品获得的id---------------------")
    console.log(id)
    router.push(`/productDetail/${id}`)
    
}
</script>

<template>
  <div class="page-container">   
    <!-- 搜索栏 -->
    <div class="search-bar">
      <input type="text" placeholder="输入商品名字" v-model="searchName" />
      <input type="number" placeholder="最低价格" v-model="searchMinPrice" />
      <input type="number" placeholder="最高价格" v-model="searchMaxPrice" />
      <button @click="getAll(1)">筛选</button>
    </div>

    <!-- 商品展示列表 -->
    <div class="product-grid" >
    
      <div class="product-card" v-for="product in productList" :key="product.proId" @click="getProductDetail(product.proId)" >
        <img :src="`${product.proImg}`" :alt="product.proName" />
        <div class="product-info">
          <h3>{{ product.proName }}</h3>
          <p class="desc">{{ product.proDesc }}</p>
          <p class="price">￥{{ product.proPrice }}</p>
        </div>
      </div>
      
    </div>

    <!-- 分页按钮 -->
    <div class="pagination">
      <button v-if="currentPage != 1" @click="getAll(currentPage - 1)">上一页</button>
      <span>{{ currentPage }} / {{ pages }}</span>
      <button v-if="currentPage != pages" @click="getAll(currentPage + 1)">下一页</button>
    </div>
  </div>    
</template>

<style scoped>
.page-container {
  max-width: 1200px;
  margin: auto;
  margin-top: 100px;
  padding: 20px;
  font-family: "Arial", sans-serif;
}

.search-bar {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.search-bar input {
  padding: 8px 10px;
  border: 1px solid #ccc;
  border-radius: 8px;
  flex: 1;
}

.search-bar button {
  padding: 8px 16px;
  background-color: #42b983;
  border: none;
  border-radius: 8px;
  color: white;
  cursor: pointer;
  transition: 0.2s;
}
.search-bar button:hover {
  background-color: #369a6e;
}

.product-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 20px;
}

.product-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  transition: transform 0.2s;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.product-card:hover {
  transform: translateY(-5px);
}

.product-card img {
  width: 100%;
  height: 180px;
  object-fit: cover;
}

.product-info {
  padding: 12px;
  text-align: center;
}
.product-info h3 {
  font-size: 16px;
  margin: 8px 0;
}
.product-info .desc {
  font-size: 14px;
  color: #666;
  margin-bottom: 8px;
}
.product-info .price {
  font-size: 18px;
  font-weight: bold;
  color: #e63946;
}

.pagination {
  margin-top: 20px;
  text-align: center;
}
.pagination button {
  padding: 6px 12px;
  margin: 0 8px;
  background-color: #42b983;
  border: none;
  border-radius: 6px;
  color: white;
  cursor: pointer;
}
.pagination button:hover {
  background-color: #369a6e;
}
</style>
